package cn.smartorigin.soas.frame;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import cn.smartorigin.soas.data.SqliteConnecter;
import cn.smartorigin.soas.data.TestData;
import cn.smartorigin.soas.frame.panel.ProductListPage;

public class ModifyClientProduct extends PopupDialogFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JButton mCfnBtn, mClrBtn;
	JTextField mClientName, mProductName, mProductPrice;
	String mClientNameStr, mProductNameStr, mProductPriceStr;
	int mClientId;
	JComboBox mPlateKind, mPlateType;
	
	ProductListPage plp;
	
	public ModifyClientProduct(int clientId,String cname ,String pname, String pprice, 	ProductListPage pl){
		super();
		this.plp = pl;
		this.setTitle(" 产品修改  - SOAS");
		this.mClientNameStr = cname;
		this.mClientId = clientId;
		this.mProductNameStr = pname;
		this.mProductPriceStr = pprice;
		this.compentInit();
	}
	

	
	private void compentInit(){
		this.setLayout(null);
		
		BtnListener btnlistener = new BtnListener();
		
		ImageIcon clrCfnIcon=new ImageIcon("res/images/ic_corfim.png");
		mCfnBtn = new JButton("确定", clrCfnIcon);
		mCfnBtn.setBounds(250,300,100,30);
		mCfnBtn.addActionListener(btnlistener);
		
		ImageIcon clrBtnIcon=new ImageIcon("res/images/ic_delete.png"); 
		mClrBtn = new JButton("取消",clrBtnIcon);
		mClrBtn.setBounds(450,300,100,30);
		mClrBtn.addActionListener(btnlistener);
		
		/**
		 * 客户名称
		 */
		JLabel clientName  = new JLabel("客户名称：");
		clientName.setBounds(250,25,100,30);
		mClientName = new JTextField(mClientNameStr);
		mClientName.setBounds(350,25,200,30);
		mClientName.setBackground(Color.white);
		mClientName.addActionListener(btnlistener);
		mClientName.setEditable(false);
		
		/**
		 * 品名
		 */
		JLabel productNamelbl  = new JLabel("材料名称：");
		productNamelbl.setBounds(250,75,100,30);
		mProductName = new JTextField(mProductNameStr);
		mProductName.setBounds(350,75,200,30);
		mProductName.setBackground(Color.white);
		
		JLabel plateTypeLbl  = new JLabel("表面处理方式：");
		plateTypeLbl.setBounds(250,125,100,30);
		try {
			mPlateType = new JComboBox(	TestData.queryPlateType(new SqliteConnecter()));
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mPlateType.setBounds(350,125,100,30);
		mPlateType.setBackground(Color.white);
		
		JLabel plateKindLbl  = new JLabel("镀种：");
		plateKindLbl.setBounds(250,175,100,30);
		String item[] = {"滚镀", "吊镀"};
		mPlateKind = new JComboBox(item);
		mPlateKind.setBounds(350,175,100,30);
		mPlateKind.setBackground(Color.white);
		
		/**
		 * 数量
		 */
		JLabel pricelbl  = new JLabel("单价 /KG：");
		pricelbl.setBounds(250,225,100,30);
		mProductPrice = new JTextField(mProductPriceStr);
		mProductPrice.setBounds(350,225,200,30);
		
		this.add(mCfnBtn);
		this.add(mClrBtn);
		this.add(clientName);
		this.add(productNamelbl);
		this.add(pricelbl);
		this.add(mClientName);
		this.add(mProductName);
		this.add(mProductPrice);
		this.add(plateTypeLbl);
		this.add(plateKindLbl);
		this.add(mPlateKind);
		this.add(mPlateType);
	}
	
	class BtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			Object  obj =(Object)e.getSource();
			if(obj == mCfnBtn){
				saveData();
//				mf.reflashData();
				frameClose();
			}else if(obj == mClrBtn){
				frameClose();
			}
		}
	}
	
	
	private  void frameClose(){
		plp.flashData();
		this.dispose();
	}
	
	private void saveData(){
		try {
			TestData.updateClientProduct(new SqliteConnecter(), 
					mClientId, 
					mProductName.getText(), 
					mProductNameStr, 
					mProductPrice.getText(),
					mPlateType.getSelectedItem().toString(),
					mPlateKind.getSelectedItem().toString());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
